﻿using Ekton_Manage_DomainCore.Aggregate;
using Ekton_Manage_DTO.APIRequestModels.SysUser;
using Ekton_Manage_DTO.RequestModels.Product;
using Ekton_Manage_DTO.RequestModels.SysUser;
using Ekton_Manage_IBusiness;
using Ekton_Manage_InfrastructureCore.Common;
using Ekton_Manage_InfrastructureCore.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Ekton_Manage_Web.Controllers
{
    /// <summary>
    /// 前端系统消息
    /// </summary>
    /// 
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize("Permission")]
    public class ApiSysMsgController : ControllerBase
    {
        private readonly IApiSelectSysMsg _apiSelectSysMsg;
        public ApiSysMsgController(IApiSelectSysMsg apiSelectSysMsg)
        {
            _apiSelectSysMsg = apiSelectSysMsg;
        }

        /// <summary>
        /// 查询用户的系统消息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<user_msg>>> GetMsglList([FromQuery] QueryDataParame query)
        {
            var result = await _apiSelectSysMsg.GetMsgList(query);

            return new MessageModel<PageModel<user_msg>>
            {
                msg = "数据获取成功",
                response = new PageModel<user_msg>
                {
                    pageIndex = query.pageIndex,
                    pageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(result.Item2) / query.pageSize)),
                    dataCount = result.Item2,
                    pageSize = query.pageSize,
                    data = result.Item1
                }
            };
        }


        /// <summary>
        /// 查询单个系统消息
        /// </summary>
        /// <remarks>
        /// look_state代表用户是否已读，0为未读，1为已读
        /// </remarks>
        [HttpGet]
        public async Task<MessageModel<PageModel<user_msg>>> GetUserMsg([FromQuery] QueryDataParame query)
        {
            var result = await _apiSelectSysMsg.GetUserMsg(query);

            return new MessageModel<PageModel<user_msg>>
            {
                msg = "数据获取成功",
                response = new PageModel<user_msg>
                {
                    pageIndex = query.pageIndex,
                    pageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(result.Item2) / query.pageSize)),
                    dataCount = result.Item2,
                    pageSize = query.pageSize,
                    data = result.Item1
                }
            };
        }

        /// <summary>
        /// 用户点击过之后，将这条消息的状态改为已读
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public async Task<Message> PutSysMsgState(int id)
        {
            return await _apiSelectSysMsg.PutSysMsgState(id);
        }

        /// <summary>
        /// 查询用户未读的消息个数
        /// </summary>
        /// <param name="user_id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Message> GetMsgCount(int user_id)
        {
            return await _apiSelectSysMsg.GetMsgCount(user_id);
        }

        /// <summary>
        /// 批量修改系统消息状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<Message> PutAllSysMsgState(int id)
        {
            return await _apiSelectSysMsg.PutAllSysMsgState(id);
        }
    }
}
